<template>
  <el-card>
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加</el-button>

      </div>
    </template>

    <el-table v-loading="loading" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange">

      <el-table-column prop="name" label="模版名称">
      </el-table-column>
      <el-table-column prop="code" label="开发语言">
      </el-table-column>


      <el-table-column label="操作" width="220">
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改</a>
          <a style="cursor: pointer; margin-right: 10px" @click="handleFileEdit(scope.row.id)">管理文件</a>
          <el-popconfirm title="确定删除吗,删除后对应的字段映射也将删除？" @confirm="handleDeleteOne(scope.row.id)">
            <template #reference>
              <a style="cursor: pointer">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination background layout="prev, pager, next" :total="total" :page-size="pageSize"
      :current-page="currentPage" @current-change="changePage" />
    <DialogAddTemplate ref='DialogAddTemplate' :reload="getDataList" :type="type" />
    <DialogTemplateFiles ref='DialogTemplateFiles' :reload="getDataList" :type="type" />

  </el-card>
</template>

<script lang="ts">
  import DialogAddTemplate from '@/components/DialogTemplateAdd.vue'
  import DialogTemplateFiles from '@/components/DialogTemplateFiles.vue'
  import {
    onMounted,
    onUnmounted,
    reactive,
    ref,
    toRefs
  } from 'vue'
  import {
    useRoute,
    useRouter
  } from 'vue-router'
  import * as db_context from '@/models/db_context'
  import {
    ElMessage
  } from 'element-plus'



  export default {
    name: 'DB',
    components: {
      DialogAddTemplate,
      DialogTemplateFiles
    },
    setup() {

      const route = useRoute()
      const router = useRouter()

      const DialogAddTemplate = ref < any > (null);
      const DialogTemplateFiles = ref < any > (null);


      const multipleTable = ref(null)

      const state = reactive({
        loading: false,
        tableData: [] as any[], // 数据列表
        multipleSelection: [], // 选中项
        total: 0, // 总条数
        currentPage: 1, // 当前页
        pageSize: 10, // 分页大小
        type: 'add'
      })

      onMounted(() => {
        getDataList();

      })



      // 获取数据列表
      const getDataList = () => {
        db_context.Template
          .getList < db_context.Template > ()
          .then(results => {
            console.log('resultsxxxxx:', results);
            state.tableData = results;
          });
      }



      const handleAdd = async () => {
        state.type = 'add' // 传入弹窗组件用于弹窗 title 判断
        DialogAddTemplate.value.open("");

      }
      // 修改分类
      const handleEdit = (id: string) => {
        state.type = 'edit' // 传入弹窗组件用于弹窗 title 判断
        DialogAddTemplate.value.open(id)
      }
      //文件管理
      const handleFileEdit = (id: string) => {
        //state.type = 'edit' // 传入弹窗组件用于弹窗 title 判断
        //DialogTemplateFiles.value.open(id)


        router.push(`/templateFile/${id}`);

      }

      // 单个删除
      const handleDeleteOne = async (id: string) => {
        await db_context.Template.deleteById(Number.parseInt(id));
        ElMessage.success('删除成功');
        getDataList();

      }

      return {
        ...toRefs(state),
        multipleTable,
        handleAdd,
        handleEdit,
        handleFileEdit,
        handleDeleteOne,
        getDataList,
        DialogAddTemplate,
        DialogTemplateFiles
      }
    }
  }
</script>

<style scoped>

</style>